import { Injectable } from '@angular/core';
import { HttpClientService } from 'app/services/http-client.service';
import { Http } from '@angular/http';
import * as moment from 'moment';

@Injectable()
export class ModifyPageService {

    constructor(
        public httpClient: HttpClientService,
        public http: Http
    ) {

    }
    // 分页列表
    list(postBody) {
        return this.httpClient.post('/pms/v1/riskmanage/investJournal/investJournalList', postBody);
    }
    // 保存
    save(postBody) {
        return this.httpClient.post('/pms/v1/riskmanage/investJournal/investJournal', postBody);
    }
    // 提交
    submit(postBody) {
        return this.httpClient.put('/pms/v1/riskmanage/investJournal/investJournal', postBody);
    }
    // 保存并提交
    saveSubmit(postBody) {
        return this.httpClient.put('/pms/v1/riskmanage/investJournal/investJournal', postBody);
    }
    // 提醒分页
    getRemindList(getBody) {
        return this.httpClient.get('/pms/1/sendhis/query', getBody);
    }
    // 基金状态
    getFundStatus() {
        return this.httpClient.get('/pms/v1/riskmanage/investJournal/getAllStatus');
    }
    // 导出指标
    exportTarget(postUrl, postBody, operate) {
        let today = moment().format('YYYY-MM-DD');
        this.http.post(postUrl, postBody, { responseType: 3 }).map(res => res.json()).subscribe(res => {
            // let blob = new Blob([res], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
            // let blob = new Blob([res], { type: "application/vnd.ms-excel" });
            let blob = new Blob([res], { type: "application/msexcel;charset=UTF-8" });
            if (window.navigator.msSaveOrOpenBlob) {
                navigator.msSaveBlob(blob, operate + today + '.xls');
            } else {
                let resUrl = URL.createObjectURL(blob);
                let a = document.createElement('a');
                document.body.appendChild(a);
                a.setAttribute('style', 'display:none');
                a.setAttribute('href', resUrl);
                a.setAttribute('download', operate + today + '.xls');
                a.click();
                // document.body.removeChild(a);
                // 释放url
                URL.revokeObjectURL(resUrl);
            }
        })
    }
}
